package states.designer
{
	import components.Link;
	
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import oboe.controllers.jml.JMLController;
	import oboe.controllers.jml.helpers.JMLButton;
	import oboe.controllers.jml.helpers.JMLInput;
	import oboe.controllers.jml.helpers.JMLNode;
	import oboe.controllers.jml.helpers.window.JMLWindow;
	
	
	public class LinkEditor extends JMLController
	{
		private var styleJML:Object = {
			"->Input":{
				"background" : true,
				"backgroundColor" : 0xFFFFFF,
				"border" : true,
				"height" : 20,
				"width"  : 250
			}
		};
		private var structureJML:Array = [
			{
				"@pipe:1":"onCloseWindow",
				"@pipe:2":"onClickOK"
			},
			{
				"Window:Popup":
				{
					"@pipe:1":"onCloseWindow",
					"@pipe:2":"onClickOK",
					"title":"Signal Editor",
					"width":400,
					"height":130,
					"visible":true,
					"showResizeButton":false,
					
					"Label:lblInfo":
					{
						"x":5,
						"y":25
					},
					
					"Label:lblSignal":
					{
						"x":25,
						"y":50,
						"text":"Event:"
					},
					
					"Input:txtSignal":
					{
						"x":100,
						"y":50
					},
					
					"Label:lblSlot":
					{
						"x":25,
						"y":75,
						"text":"Action:"
					},

					"Input:txtSlot":
					{
						"x":100,
						"y":75
					},
					
					"Button:OK":
					{
						"x":295,
						"y":100,
						"width":100,
						"height":25,
						"text":"OK"
					}
				}
			}
		];
		
		public function LinkEditor():void
		{
			super( null, styleJML );
			this.addControllerType("Window", JMLWindow);
			this.addDisplayObjectType("Button", JMLButton);
			this.addControllerType("Input", JMLInput);
			this.loadStructure( structureJML );
		}
		
		private var link:DesignerLink;
		[slot] public function editLink( link:DesignerLink ):void
		{
			var parent:DisplayObjectContainer = this.getDisplayObject().parent;
			parent.setChildIndex( this.getDisplayObject(), parent.numChildren-1 );
			
			this.link = link;
			var names:Array = [];
			this.link.messageStart("getText", names);
			this.link.messageEnd("getText", names);
			
			this.getNodeFromId("lblInfo").setProperty("text", "Editing link [" + names[0] + "]->[" + names[1] + "]" );
			var popup:JMLNode = this.getNodeFromId("Popup");
			var pos:Point = new Point();
			link.getPosition(pos);
			popup.setProperty("visible", true);
			popup.setProperty("x", pos.x);
			popup.setProperty("y", pos.y);
			var di:Object = this.link.getPerspectiveData();
			this.getNodeFromId("txtSignal").setProperty("text", di.start || "");
			this.getNodeFromId("txtSlot").setProperty("text", di.end || "");
		}

		[slot] public function onClickOK():void
		{
			this.link.setPerspectiveData( 
				{ 
					start:this.getNodeFromId("txtSignal").getProperty("text"),
					end: this.getNodeFromId("txtSlot").getProperty("text")
				});
			onCloseWindow();
		}
		
		[slot] public function onCloseWindow():void
		{
			this.getNodeFromId("Popup").setProperty("visible", false);
		}
	}
}